﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;

namespace PeekService
{
   public class OpLogService
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
       public bool Add(Oplog model)
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append("insert into Oplog(");
           strSql.Append("Code,State,UserName,OperateDesc,OperateType,IPAddress,UserMac,UserComputerName,CreateDate,Remark");
           strSql.Append(") values (");
           strSql.Append("@Code,@State,@UserName,@OperateDesc,@OperateType,@IPAddress,@UserMac,@UserComputerName,@CreateDate,@Remark");
           strSql.Append(") ");

           SqlParameter[] parameters = {
			            new SqlParameter("@Code", SqlDbType.VarChar,36) ,            
                        new SqlParameter("@State", SqlDbType.Int,4) ,            
                        new SqlParameter("@UserName", SqlDbType.NVarChar,36) ,            
                        new SqlParameter("@OperateDesc", SqlDbType.NVarChar,200) ,            
                        new SqlParameter("@OperateType", SqlDbType.NChar,10) ,            
                        new SqlParameter("@IPAddress", SqlDbType.Char,15) ,            
                        new SqlParameter("@UserMac", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@UserComputerName", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@CreateDate", SqlDbType.DateTime) ,            
                        new SqlParameter("@Remark", SqlDbType.NVarChar,200)             
              
            };

           parameters[0].Value = model.Code;
           parameters[1].Value = model.State;
           parameters[2].Value = model.UserName;
           parameters[3].Value = model.OperateDesc;
           parameters[4].Value = model.OperateType;
           parameters[5].Value = model.IPAddress;
           parameters[6].Value = model.UserMac;
           parameters[7].Value = model.UserComputerName;
           parameters[8].Value = model.CreateDate;
           parameters[9].Value = model.Remark;
           int rows = DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
           if (rows > 0)
           {
               return true;
           }
           else
           {
               return false;
           }

       }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Oplog model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Oplog set ");

            strSql.Append(" Code = @Code , ");
            strSql.Append(" State = @State , ");
            strSql.Append(" UserName = @UserName , ");
            strSql.Append(" OperateDesc = @OperateDesc , ");
            strSql.Append(" OperateType = @OperateType , ");
            strSql.Append(" IPAddress = @IPAddress , ");
            strSql.Append(" UserMac = @UserMac , ");
            strSql.Append(" UserComputerName = @UserComputerName , ");
            strSql.Append(" CreateDate = @CreateDate , ");
            strSql.Append(" Remark = @Remark  ");
            strSql.Append(" where Code=@Code  ");

            SqlParameter[] parameters = {
			            new SqlParameter("@Code", SqlDbType.VarChar,36) ,            
                        new SqlParameter("@State", SqlDbType.Int,4) ,            
                        new SqlParameter("@UserName", SqlDbType.NVarChar,36) ,            
                        new SqlParameter("@OperateDesc", SqlDbType.NVarChar,200) ,            
                        new SqlParameter("@OperateType", SqlDbType.NChar,10) ,            
                        new SqlParameter("@IPAddress", SqlDbType.Char,15) ,            
                        new SqlParameter("@UserMac", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@UserComputerName", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@CreateDate", SqlDbType.DateTime) ,            
                        new SqlParameter("@Remark", SqlDbType.NVarChar,200)             
              
            };

            parameters[0].Value = model.Code;
            parameters[1].Value = model.State;
            parameters[2].Value = model.UserName;
            parameters[3].Value = model.OperateDesc;
            parameters[4].Value = model.OperateType;
            parameters[5].Value = model.IPAddress;
            parameters[6].Value = model.UserMac;
            parameters[7].Value = model.UserComputerName;
            parameters[8].Value = model.CreateDate;
            parameters[9].Value = model.Remark;
            int rows = DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string Code)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Oplog ");
            strSql.Append(" where Code=@Code ");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,36)			};
            parameters[0].Value = Code;


            int rows = DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Oplog GetModel(string Code)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Code, State, UserName, OperateDesc, OperateType, IPAddress, UserMac, UserComputerName, CreateDate, Remark  ");
            strSql.Append("  from Oplog ");
            strSql.Append(" where Code=@Code ");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,36)			};
            parameters[0].Value = Code;
            Oplog model = new Oplog();
            using (SqlDataReader dr = DBHelper.ExecuteReader(strSql.ToString(), parameters))
            {

                if (dr.Read())
                {
                    return DataRowToModel(dr);
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 获取所有分页
        /// </summary>
        /// <returns></returns>
        public List<Oplog> GetAll(int pageIndex, int pageSize, out int RecordCount)
        {
            string strsql = @"select   Code,TourismCode,Title,Content,Remark,State,CreateID,CreateTime,Sort from MapInfo order by sort desc";
            List<Oplog> list = new List<Oplog>();
            using (SqlDataReader dr = DBHelper.ExecuteReader(strsql, pageIndex, pageSize, out RecordCount))
            {
                while (dr.Read())
                {
                    list.Add(DataRowToModel(dr));
                }
            }
            return list;
        }
        /// <summary>
        /// 获取所有
        /// </summary>
        public List<Oplog> GetAll(string strWhere)
        {
            string strSql ="select Code, State, UserName, OperateDesc, OperateType, IPAddress, UserMac, UserComputerName, CreateDate, Remark  from oplog  ";
            List<Oplog> list = new List<Oplog>();
            using (SqlDataReader dr = DBHelper.ExecuteReader(strSql))
            {
                while (dr.Read())
                {
                    list.Add(DataRowToModel(dr));
                }
            }
            return list;
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Oplog DataRowToModel(SqlDataReader reader)
        {
            Oplog model = new Oplog();
            if (reader["Code"] != DBNull.Value)
            {
                model.Code = reader["Code"].ToString();
            }
            if (reader["State"].ToString() != "")
            {
                model.State = int.Parse(reader["State"].ToString());
            }
            if (reader["UserName"].ToString() != "")
            {
                model.UserName = reader["UserName"].ToString();
            }
            if (reader["State"].ToString() != "")
            {
                model.OperateDesc = reader["OperateDesc"].ToString();
            }
            if (reader["OperateType"].ToString() != "")
            {
                model.OperateType = reader["OperateType"].ToString();
            }
            if (reader["IPAddress"].ToString() != "")
            {
                model.IPAddress = reader["IPAddress"].ToString();
            }
            if (reader["UserMac"].ToString() != "")
            {
                model.UserMac = reader["UserMac"].ToString();
            }
            if (reader["UserComputerName"].ToString() != "")
            {
                model.UserComputerName = reader["UserComputerName"].ToString();
            }
            if (reader["CreateDate"].ToString() != "")
            {
                model.CreateDate = DateTime.Parse(reader["CreateDate"].ToString());

            }
            return model;
        }
    }
}
